package com.example.androidexample.qr;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.androidexample.R;
import com.example.basic.base.BaseActivity;
import com.example.router.ActivityRouter;

public class QRActivity extends BaseActivity implements SurfaceHolder.Callback {

    private QRView qr_view;

    private static final String TAG  = "QRView";
    private SurfaceView surfaceView;
    private SurfaceHolder mSurfaceHolder;
    private TextView tv_result;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 200) {
                String text = (String) msg.obj;
                tv_result.setText(text);
            }
        }
    };

    public void startScan(View view) {
        CameraController.getInstance().startFrameDecode(handler);
    }

    public void stopScan(View view) {
        CameraController.getInstance().stopFrameDecode();
    }

    @Override
    protected void onResume() {
        Log.d(TAG, "onResume: ");
        super.onResume();
        Log.d(TAG, "onResume: ");
        qr_view.startScan();
    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        Log.d(TAG, "initOnCreate: ");
        qr_view = findViewById(R.id.qr_view);
        surfaceView = findViewById(R.id.qr_camera_preview);
        surfaceView.getHolder().addCallback(this);
        tv_result = findViewById(R.id.tv_result);
    }

    @Override
    protected int setLayoutXmlid() {
        return R.layout.activity_qr_scan;
    }

    @Override
    public String getTag() {
        return "QRActivity";
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: ");
        qr_view.stopScan();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");

    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated: ");
        mSurfaceHolder = holder;
        //检查权限  （以后再补）
        CameraController.getInstance().openFacingBack(mSurfaceHolder);
        CameraController.getInstance().startFrameDecode(handler);

    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
        Log.d(TAG, "surfaceChanged: ");
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        Log.d(TAG, "surfaceDestroyed: ");
        CameraController.getInstance().closeCamera();
    }
}
